package com.iplant.mes.controller.sfc;

import com.alibaba.fastjson.JSON;
import com.iplant.aps.service.apsenum.aps.APSFirstCheckStatus;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.SFCFrockBindService;
import com.iplant.mes.service.SFCService;
import com.iplant.mes.service.po.bcm.BCMRuleCode;
import com.iplant.mes.service.po.constants.MESConstants;
import com.iplant.mes.service.po.ipt.IPTValue;
import com.iplant.mes.service.po.sfc.SFCStationInterfaceLog;
import com.iplant.mes.service.po.sfc.SFCTaskIPT;
import com.iplant.mes.service.po.sfc.SFCTaskSelf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/api/SFCTaskIPT")
public class SFCTaskIPTController extends BasicController<SFCTaskIPT> {
    private static final Logger logger = LoggerFactory.getLogger(SFCTaskIPTController.class);

    public SFCTaskIPTController() {
        super(SFCTaskIPT.class);
    }

    @Autowired
    SFCService mSFCService;

    @Autowired
    SFCFrockBindService mSFCFrockBindService;

    PoService<SFCTaskSelf> mSFCTaskSelfService = ORMUtils.GetPoService(SFCTaskSelf.class);

    /**
     * 根据工位、工件号创建检验单（自检、互检）
     */
    @PostMapping("/CreateTaskByWorkpieceNo")
    public Object CreateTaskByWorkpieceNo(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wStationID = StringUtils.parseInt(wParam.get("StationID"));
            int wCheckType = StringUtils.parseInt(wParam.get("CheckType"));
            String wWorkpieceNo = StringUtils.parseString(wParam.get("WorkpieceNo"));
            if (wStationID <= 0 || wCheckType <= 0 || StringUtils.isEmpty(wWorkpieceNo)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<SFCTaskIPT> wServiceResult = mSFCService.SFC_CreateTaskByWorkpieceNo(wLoginUser, wStationID
                    , wCheckType, wWorkpieceNo);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 根据工位、工件号创建检验单（自检、互检）
     */
    @PostMapping("/CreateTaskByWorkpieceNoAndTask")
    public Object CreateTaskByWorkpieceNoAndTask(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wStationID = StringUtils.parseInt(wParam.get("StationID"));
            int wCheckType = StringUtils.parseInt(wParam.get("CheckType"));
            String wWorkpieceNo = StringUtils.parseString(wParam.get("WorkpieceNo"));
            SFCTaskSelf wSFCTaskSelf = CloneTool.Clone(wParam.get("data"), SFCTaskSelf.class);

            if (wStationID <= 0 || wCheckType <= 0 || StringUtils.isEmpty(wWorkpieceNo) || wSFCTaskSelf == null || wSFCTaskSelf.ID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            //查询工单，得到最新的数据
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);

            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("ID", wSFCTaskSelf.ID);
            wSFCTaskSelf = mSFCTaskSelfService.SelectPO(wLoginUser, wParams,
                    wErrorCode);

            //判断是否做了首检（若没做，则报错）
            if (wSFCTaskSelf.FirstCheckStatus == APSFirstCheckStatus.Locked.getValue()
                    || wSFCTaskSelf.FirstCheckStatus == APSFirstCheckStatus.CheckBad.getValue()
            ) {
                return GetResult(RetCode.SERVER_CODE_ERR, StringUtils.Format("操作失败：工单【{0}】未首检或首检不合格!!!",
                        wSFCTaskSelf.Code));
            }

            ServiceResult<SFCTaskIPT> wServiceResult = mSFCService.SFC_CreateTaskByWorkpieceNoAndTask(wLoginUser,
                    wStationID
                    , wCheckType, wWorkpieceNo, wSFCTaskSelf);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, wServiceResult.FaultCode, null, wServiceResult.Result);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 提交检验值
     */
    @PostMapping("/SubmitValue")
    public Object SubmitValue(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<IPTValue> wIPTValueList = CloneTool.CloneArray(wParam.get("data"), IPTValue.class);
            if (wIPTValueList == null || wIPTValueList.size() == 0 || wIPTValueList.stream().anyMatch(p -> p.TaskID <= 0)) {
                return GetResult(RetCode.SERVER_CODE_ERR, "未提交检验值，请检查是否作业项点未配置!");
            }

            Calendar wRequestTime = Calendar.getInstance();

            boolean wIsDevice = false;
            ServiceResult<Integer> wServiceResult = mSFCService.IPT_SubmitValue(wLoginUser, wIPTValueList, wIsDevice,
                    false);

            Calendar wResponseTime = Calendar.getInstance();

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }

            SFCTaskIPT wSFCTaskIPT = (SFCTaskIPT) wServiceResult.CustomResult.get("SFCTaskIPT");

            //维护工位实时接口日志
            Optional<SFCStationInterfaceLog> wOption =
                    MESConstants.StationInterfaceLogList.stream().filter(p -> p.StationID == wSFCTaskIPT.StationID
                            && p.InterfaceType.equals("自检结果提交")).findFirst();
            if (wOption.isPresent()) {
                SFCStationInterfaceLog sfcStationInterfaceLog = wOption.get();
                sfcStationInterfaceLog.OperatorID = wLoginUser.ID;
                sfcStationInterfaceLog.OperatorName = wLoginUser.Name;
                sfcStationInterfaceLog.InterfaceInput = JSON.toJSONString(wParam);
                sfcStationInterfaceLog.InterfaceOutput = JSON.toJSONString(wResult);
                sfcStationInterfaceLog.RequestTime = wRequestTime;
                sfcStationInterfaceLog.ResponseTime = wResponseTime;
            } else {
                SFCStationInterfaceLog wSFCStationInterfaceLog =
                        new SFCStationInterfaceLog(wSFCTaskIPT.StationID, wSFCTaskIPT.StationName,
                                wSFCTaskIPT.StationCode, wLoginUser.ID, wLoginUser.Name,
                                JSON.toJSONString(wParam),
                                JSON.toJSONString(wResult), wRequestTime,
                                wResponseTime
                                , "自检结果提交");
                MESConstants.StationInterfaceLogList.add(wSFCStationInterfaceLog);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 根据订单、工件创建专检单
     */
    @PostMapping("/CreateSpecialCheckTaskByOrder")
    public Object CreateSpecialCheckTaskByOrder(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wOrderID = StringUtils.parseInt(wParam.get("OrderID"));
            int wRoutePartID = StringUtils.parseInt(wParam.get("RoutePartID"));
            int wRoutePartPointID = StringUtils.parseInt(wParam.get("RoutePartPointID"));
            String wWorkpieceNo = StringUtils.parseString(wParam.get("WorkpieceNo"));
            if (wOrderID <= 0 || wRoutePartID <= 0 || wRoutePartPointID <= 0 || StringUtils.isEmpty(wWorkpieceNo)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<SFCTaskIPT> wServiceResult = mSFCService.SFC_CreateSpecialCheckTaskByOrder(wLoginUser,
                    wOrderID
                    , wRoutePartID, wRoutePartPointID, wWorkpieceNo);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 创建抽检单（重做首检）
     */
    @PostMapping("/CreateSpotCheckTaskByWorkpieceNoAndTask")
    public Object CreateSpotCheckTaskByWorkpieceNoAndTask(HttpServletRequest request, @RequestBody Map<String,
            Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wWorkpieceNo = StringUtils.parseString(wParam.get("WorkpieceNo"));
            SFCTaskSelf wSFCTaskSelf = CloneTool.Clone(wParam.get("data"), SFCTaskSelf.class);
            if (StringUtils.isEmpty(wWorkpieceNo) || wSFCTaskSelf == null || wSFCTaskSelf.ID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }
            ServiceResult<SFCTaskIPT> wServiceResult =
                    mSFCFrockBindService.SFC_CreateSpotCheckTaskByWorkpieceNoAndTask(wLoginUser,
                            wWorkpieceNo, wSFCTaskSelf);
            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 创建复检单（重做自检）
     */
    @PostMapping("/CreateReCheckTaskByWorkpieceNoAndTask")
    public Object CreateReCheckTaskByWorkpieceNoAndTask(HttpServletRequest request, @RequestBody Map<String,
            Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wWorkpieceNo = StringUtils.parseString(wParam.get("WorkpieceNo"));
            SFCTaskSelf wSFCTaskSelf = CloneTool.Clone(wParam.get("data"), SFCTaskSelf.class);
            if (StringUtils.isEmpty(wWorkpieceNo) || wSFCTaskSelf == null || wSFCTaskSelf.ID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }
            ServiceResult<SFCTaskIPT> wServiceResult =
                    mSFCFrockBindService.SFC_CreateReCheckTaskByWorkpieceNoAndTask(wLoginUser,
                            wWorkpieceNo, wSFCTaskSelf);
            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 验证所填写的工件码/芯子码/成品码/外壳码是否合法（组装工序）
     */
    @PostMapping("/CheckWorkpieceValue")
    public Object CheckWorkpieceValue(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            SFCTaskIPT wSFCTaskIPT = CloneTool.Clone(wParam.get("SFCTaskIPT"), SFCTaskIPT.class);
            IPTValue wIPTValue = CloneTool.Clone(wParam.get("IPTValue"), IPTValue.class);

            if (wSFCTaskIPT == null || wSFCTaskIPT.ID == 0 || StringUtils.isEmpty(wIPTValue.Value) || wIPTValue.ComponentType <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<String> wServiceResult = mSFCService.SFC_CheckWorkpieceValue(wLoginUser, wSFCTaskIPT,
                    wIPTValue);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 获取工控机主界面数据
     */
    @GetMapping("/QueryMainInterfaceDataAll")
    public Object QueryMainInterfaceDataAll(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String ipAddress = request.getHeader("X-Forwarded-For");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
            }
            if (StringUtils.isEmpty(ipAddress)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            int wShiftType = StringUtils.parseInt(request.getParameter("ShiftType"));

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_QueryMainInterfaceDataAll(wLoginUser, ipAddress,
                    wShiftType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 查询复工列表
     */
    @GetMapping("/QueryResumptionList")
    public Object QueryResumptionList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String ipAddress = request.getHeader("X-Forwarded-For");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
            }
            if (StringUtils.isEmpty(ipAddress)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            int wShiftType = StringUtils.parseInt(request.getParameter("ShiftType"));

            ServiceResult<List<SFCTaskSelf>> wServiceResult = mSFCService.SFC_QueryResumptionList(wLoginUser,
                    ipAddress, wShiftType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 根据工件查询可专检的工序任务列表
     */
    @GetMapping("/QueryToSpecialTaskList")
    public Object QueryToSpecialTaskList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wComponentCode = StringUtils.parseString(request.getParameter("ComponentCode"));
            if (StringUtils.isEmpty(wComponentCode)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<SFCTaskIPT>> wServiceResult = mSFCService.SFC_QueryToSpecialTaskList(wLoginUser,
                    wComponentCode);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量删除
     */
    @Override
    @PostMapping("/DeleteList")
    public Object DeleteList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            List<SFCTaskIPT> wSFCTaskIPTList = CloneTool.CloneArray(wParam.get("data"), SFCTaskIPT.class);
            if (wSFCTaskIPTList == null || wSFCTaskIPTList.size() == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_DeleteTaskIPTList(wLoginUser, wSFCTaskIPTList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 删除
     */
    @Override
    @PostMapping("/Delete")
    public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            SFCTaskIPT wSFCTaskIPT = CloneTool.Clone(wParam.get("data"), SFCTaskIPT.class);
            if (wSFCTaskIPT == null || wSFCTaskIPT.ID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_DeleteTaskIPT(wLoginUser, wSFCTaskIPT);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 生成条码
     */
    @PostMapping("/CreateBarCodeByTask")
    public Object CreateBarCodeByTask(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            SFCTaskSelf wSFCTaskSelf = CloneTool.Clone(wParam.get("data"), SFCTaskSelf.class);
            if (wSFCTaskSelf == null || wSFCTaskSelf.ID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<BCMRuleCode> wServiceResult = mSFCService.SFC_CreateBarCodeByTask(wLoginUser, wSFCTaskSelf);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
